package sunwin.yois.web.controller;

import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import sunwin.yois.web.common.R;
import sunwin.yois.web.common.YoisImageHelper;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.comm.YomsPage;
import sunwin.yoms.comm.model.NameAndValueModel;
import sunwin.yoms.domain.publicty.NewsDomain;
import sunwin.yoms.service.publicty.NewsService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ocmt on 2015-9-3.
 */
@Controller
public class NewsController {
    @Autowired
    private NewsService newsService;
    /**
     * 访问新闻首页
     *
     * @param modelMap
     * @param tid         // 新闻id
     * @param pix         // 页码
     */
//  用于类上，表示类中的所有  响应请求  的方法都是以该地址作为父路径
    @RequestMapping(value = R.NewsIndex, name = "news/", method = RequestMethod.GET)
    //value：     指定请求的实际地址，指定的地址可以是URI Template 模式（后面将会说明）；
    //method：    指定请求的method类型， GET、POST、PUT、DELETE等；
    public void news(ModelMap modelMap,
                     @RequestParam(required = false, defaultValue = "2") int tid,
                     @RequestParam(required = false, defaultValue = "1") int pix) {
        modelMap.addAttribute("tid", tid);
        modelMap.addAttribute("pix", pix);

        getNewsByType(modelMap, tid, pix);
        getNewsByHot(modelMap);
    }

    /**
     * 根据新闻的类型获取分页新闻
     *
     * @param modelMap
     * @param tid
     * @param pix
     */
    private void getNewsByType(ModelMap modelMap, int tid, int pix) {
        YomsPage yomspage = new YomsPage();
        yomspage.setPageSize(20);
        yomspage.setPageIndex(pix);

        List<NewsDomain> domains = newsService.getNewsByTypeId(tid, yomspage);
        for (NewsDomain domain : domains) {
            String content = domain.getNewsContent();
            String newContent = YoisImageHelper.delHtmlTag(content);
            if (newContent.length() > 200) {
                String aTag = String.format("<a href='%s?nid=%s'>详细</a>", R.getNewsDetailed(), domain.getNewsId());
                newContent = newContent.substring(0, 200) + "..." + aTag;
            }
            domain.setNewsContent(newContent);
        }

        List<NameAndValueModel> pages = new ArrayList<>(0);
        for (int i = 0; i < yomspage.getPageCount(); i++) {
            NameAndValueModel mavm = new NameAndValueModel();
            mavm.setValue(String.format("/%s?tid=%s&pix=%s", R.getNewsIndex(), tid, i + 1));
            mavm.setName(i + 1 + "");
            pages.add(mavm);
            if (i == 9) {
                break;
            }
        }

        modelMap.addAttribute("pages", pages);
        modelMap.addAttribute("newsList", domains);
        modelMap.addAttribute("pol", yomspage.getPageCount());
    }

    /**
     * 根据热度查找10条新闻
     * @param modelMap
     */
    private void getNewsByHot(ModelMap modelMap) {
        List<NewsDomain> domains = newsService.getNewsByHot(9);
        modelMap.addAttribute("hotNews", domains);
    }

    /**
     * 访问单条新闻的页面
     *
     * @param modelMap
     * @param nid
     */
    @RequestMapping(value = R.NewsDetailed, name = "news/", method = RequestMethod.GET)
    public void newsDetailed(ModelMap modelMap,
                             @RequestParam(required = false, defaultValue = "0") int nid) {
        try {
            modelMap.addAttribute("nid", nid);

            NewsDomain newsDomain = newsService.getNewsById(nid);
            String content = YoisImageHelper.changeImgSrc(newsDomain.getNewsContent(), "detailed");
            newsDomain.setNewsContent(content);

            if (StringUtil.isBlank(newsDomain.getNewsContent())) {
                newsDomain.setNewsContent("...");
            }
            modelMap.addAttribute("newsDomain", newsDomain);
            //无样式新闻内容
            String newContent = YoisImageHelper.delHtmlTag(content);
            modelMap.addAttribute("newsContent", newContent);

            newsService.updateNewsHot(nid);
        } catch (YomsException e) {

        }
    }
}